<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="园区" prop="area">
        <el-select v-model="queryParams.location" placeholder="请选择园区" clearable style="width: 150px" @change="handleQuery">
          <el-option label="全部" value="" ></el-option>
          <el-option v-for="item in dict.type.user_area" :label="item.label" :value="item.value" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="用户昵称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入用户昵称"
          clearable
          style="width: 150px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始日期" prop="selectDate">
        <el-date-picker
          v-model="queryParams.startDate"
          type="date"
          value-format="yyyy-MM-dd"
          style="width: 150px"
          placeholder="开始日期">
        </el-date-picker>
<!--        <el-date-picker-->
<!--          v-model="queryParams.selectDate"-->
<!--          type="daterange"-->
<!--          range-separator="至"-->
<!--          start-placeholder="开始日期"-->
<!--          end-placeholder="结束日期">-->
<!--        </el-date-picker>-->
      </el-form-item>
      <el-form-item label="结束日期" prop="customerName">
        <el-date-picker
          v-model="queryParams.endDate"
          type="date"
          value-format="yyyy-MM-dd"
          style="width: 150px"
          placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item >
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="rowList">
      <el-table-column label="用户编号" align="center" prop="customerId" width="120" fixed="left"/>
      <el-table-column label="用户昵称" align="center" prop="customerName" width="120" fixed="left"/>
      <el-table-column label="园区" align="center" prop="location" width="120"/>
      <el-table-column label="金额" align="center" prop="amount" width="120">
        <template v-slot="{row}">
          <span style="color: red" v-if="row.type==='用户消费'|| row.type==='手动减少'">{{row.amount | currency}}</span>
          <span v-else>{{row.amount | currency}}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" width="120"/>
      <el-table-column label="原因" align="center" prop="reason" min-width="400"/>
      <el-table-column label="创建人" prop="createBy" width="120" align="center" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime,'{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="120" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="text"  icon="el-icon-download" size="mini" @click="rowExport(row)">打印小票</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { list, get, update } from "@/api/shop/balanceDetail";
import moment from "moment/moment";
export default {
  name: "Detail",
  dicts: ['user_area'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 食材表格数据
      rowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openBalance: false,
      // 是否显示弹出层（数据权限）
      openDataScope: false,
      menuExpand: false,
      menuNodeAll: false,
      deptExpand: true,
      deptNodeAll: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        location:'',
        customerName:'',
        startDate:'',
        endDate:'',
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.queryParams.location=this.$store.state.user.userInfo.area || ''
    this.queryParams.startDate=moment().subtract(1, 'months').format('yyyy-MM-DD')
    this.queryParams.endDate=moment().format('yyyy-MM-DD')
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      this.queryParams.appSource = "pc";
      list(this.queryParams).then(response => {
          this.rowList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.startDate=moment().subtract(1, 'months').format('yyyy-MM-DD')
      this.queryParams.endDate=moment().format('yyyy-MM-DD')
      this.handleQuery();
    },
    handleExport() {
      this.download('/shop/customer/balance/export', {
        ...this.queryParams
      }, `消费明细${moment().format('YYYY-MM-DD')}.xlsx`)
    },
    //打印小票
    rowExport(row){
      this.$router.push({
        path:'/jmreport/consumption',
        query:{
          id:'931398146240327680',
          rowId:row.id
        }
      })
    },
  }
};
</script>
